/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package gffparser;

import java.io.*;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 *
 * @author nadaalhirabi
 */
public class GFFParser {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws FileNotFoundException, IOException {
    // TODO code application logic here

// reading the file names from the user
        Scanner User_input = new Scanner(System.in);
        String Gff_file_name;

        System.out.print("Enter the Gff file name:");
        Gff_file_name = User_input.next();

//---------- parsing the gff file ------------    
        String token1 = "";
        List<String> temps;
        int gfflength = 0;
        try (Scanner inFile1 = new Scanner(new File(Gff_file_name)).useDelimiter("\n")) {
            temps = new LinkedList<>();
            while (inFile1.hasNext()) {
                // find next line
                token1 = inFile1.next();
                temps.add(token1);
                gfflength++;

            }
        }

        String[] tempsArray = temps.toArray(new String[0]);
        String[] gffdigested = new String[gfflength];

        int i = 0;
        for (String s : tempsArray) {//loop for all the file// each index of tempsArray array is line // so we extract line by line
            boolean TypeIsGene = false;
            String ID = "";

            if (s.equals("##gff-version 3")) {
                continue;
            }
//        if( s.equals( "##FASTA" )) break;
//        if( s.equals("null") ) break;

            int index = 0;
            for (String retval : s.split("\t")) {//loop for each line to extract the desired feature
                String type;
                if (index == 2) { // to take the type of the feature
                    type = retval;
                    gffdigested[i] = type + "\t";
                        //System.out.println(type);
                    if (type.equals("gene")) {
                        TypeIsGene = true;
                        //System.out.println(type);
                    } else {
                        TypeIsGene = false;
                    }
                }//end type 

                if (index == 3) { // to take the start of the feature
                    String start = retval;
                    gffdigested[i] += start + "\t";
                    //System.out.println(start);
                }

                if (index == 4) {
                    String end = retval; // to take the end of the feature
                    gffdigested[i] += end + "\t";
                    // System.out.println(end);
                }

                if (index == 8) {
                    int index2 = 0;
                    for (String idarray : retval.split(";")) {
                        if (index2 == 0) {
                            String id = idarray;
                            //for extracting the id without addition on it like _blabla
                            String[] id2 = idarray.split("=");
                            gffdigested[i] += id2[1];
                        }
                        index2++;
                    }
                }//end index 8

                index++;
            } //end the loop for each line to extract the desired feature
            ++i;

        } //end loop on gff
//  gffdigested[++i]="##END";

        writeTofile(gffdigested,Gff_file_name+"GFFExtracted");

    // TODO code application logic here
    }//end main

    public static void writeTofile(String[] digestedfile, String filename) throws FileNotFoundException, IOException {

        System.out.println(digestedfile[1]);

        BufferedWriter writer = null;
        try {

            File file = new File(filename + ".txt");
            PrintWriter out = new PrintWriter(new FileWriter(file));

            // Write each string in the array on a separate line  
            for (int i = 0; i < digestedfile.length; ++i) {
                String s = digestedfile[i];
                out.println(s);
            //if(s.equals("##END"))break;

            }

            out.close();

        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }//end write to file

}
